package com.deyou.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.deyou.Constants;
import com.deyou.DeYouApplication;
import com.deyou.MainActivity;
import com.deyou.R;
import com.deyou.activity.HouseResourceDetailActivity;
import com.deyou.activity.LoginActivity;
import com.deyou.model.HomeDataBean;
import com.deyou.model.HomePageCityBean;
import com.deyou.util.AndroidUtils;
import com.deyou.util.GlideImageLoader;
import com.deyou.util.SPUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yangyuancheng on 2017/5/12.
 */

public class HomePageRecommendAdapter extends RecyclerView.Adapter<HomePageRecommendAdapter.ViewHolder> {
    private Context mContext;
    List<HomeDataBean.HouseResource> data;
//    private OnCollectItemClickListener collectListener;

    public HomePageRecommendAdapter(Context context) {
        this.mContext = context;
    }

    public List<HomeDataBean.HouseResource> getData() {
        return data;
    }

    public void setData(List<HomeDataBean.HouseResource> data) {
        if (null == data)
            data = new ArrayList<>();
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recommond_homestay_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        RelativeLayout.LayoutParams rootParams = null;
        if (null != data && data.size() > 0){
            final HomeDataBean.HouseResource houseResource = data.get(position);
            if (null != houseResource){
                new GlideImageLoader().displayImage(mContext, TextUtils.isEmpty(data.get(position).resourcePic) ? R.drawable.img_default : data.get(position).resourcePic, holder.ivRecommendHomestay);
                holder.tvHomestay.setText(data.get(position).resourceName);
                holder.tvPrice.setText(Constants.RMB_UNIT + data.get(position).resourcePrice);
                holder.tvHomestayInfo.setText(data.get(position).resourceTypeName + "-" + data.get(position).resourceCityName);
                holder.tvComments.setText(data.get(position).evaluateNum + "条评论");
                holder.starLayout.removeAllViews();
                for (int i = 0; i < 5; i++) {
                    ImageView ivStar = new ImageView(mContext);
                    if (i < houseResource.evaluateStar) {
                        ivStar.setImageResource(R.drawable.ic_star_sel);
                    } else {
                        ivStar.setImageResource(R.drawable.ic_star_nor);
                    }
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(44, 40);
                    ivStar.setLayoutParams(params);
                    ivStar.setPadding(5, 0, 5, 0);
                    holder.starLayout.addView(ivStar);
                }
            }
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("resourceCode", data.get(position).resourceId);
                    AndroidUtils.startNextActivity((Activity)mContext, HouseResourceDetailActivity.class, bundle, false);
                }
            });
            if (data.size() == 1) {
                rootParams = new RelativeLayout.LayoutParams(DeYouApplication.mWidth - 80, ViewGroup.LayoutParams.WRAP_CONTENT);
                rootParams.setMargins(40, 0, 40, 0);
            } else {
                rootParams = new RelativeLayout.LayoutParams(DeYouApplication.mWidth - 200, ViewGroup.LayoutParams.WRAP_CONTENT);
                if (position == data.size() - 1)
                    rootParams.setMargins(40, 0, 40, 0);
                else
                    rootParams.setMargins(40, 0, 0, 0);
            }
            holder.rootView.setLayoutParams(rootParams);
        }
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getItemCount() {
        return null == data ? 0 : data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_recommend_homestay)
        ImageView ivRecommendHomestay;
        @BindView(R.id.iv_collect_label)
        ImageView ivCollectLabel;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_homestay)
        TextView tvHomestay;
        @BindView(R.id.tv_homestay_info)
        TextView tvHomestayInfo;
        @BindView(R.id.iv_homestay_level)
        ImageView ivHomestayLevel;
        @BindView(R.id.tv_comments)
        TextView tvComments;
        @BindView(R.id.star_layout)
        LinearLayout starLayout;
        @BindView(R.id.root_view)
        RelativeLayout rootView;

        ViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);
            ButterKnife.bind(this, view);
        }
    }

//    public interface OnCollectItemClickListener{
//        void collectItemClick(HomeDataBean.HouseResource houseResource);
//    }
//
//    public void setCollectItemClick(OnCollectItemClickListener collectListener){
//        this.collectListener = collectListener;
//    }
}
